package dao.impl;

import cqcet.aibd.soft.ObjectUtil;
import dao.AdminDAO;
import model.Goods;
import model.Order;
import model.User;


public class AdminDAOImpl implements AdminDAO {

    @Override
    public <T> T getT(Class<T> c) {
        T t = null;
        try {
            t = c.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return t;
    }

    @Override
    public int orderUpdate(Order order, String name, String address, String phone) {
        String sql = "update `order` set " +
                "id=?,serialNumber=?,isDel=?,createTime=?,deleteTime=?,user_id=?,consignee=?,consigneeAddress=?,phone=?,amount=?,state=? " +
                "where id=?";
        return new ObjectUtil<Order>().update(sql,
                order.getId(),order.getSerialNumber(),"1",order.getCreateTime(),order.getDeleteTime(), order.getUserId(),name,address,phone,order.getAmount(),order.getState(),
                order.getId());

    }

    @Override
    public int userDelete(String id) {
        String sql = "delete from user where id = ? ";
        return new ObjectUtil<User>().delete(sql,id);
    }

    @Override
    public int userUpdateLevel(User user, String role) {
        String sql = "update user set id=?,createTime=?,deleteTime=?,isDel=?,username=?,password=?,role=? where id=?";

        return new ObjectUtil<User>().update(sql,user.getId(),user.getCreateTime(),user.getDeleteTime(),user.getIsDel(),user.getUsername(),
                user.getPassword(),role,user.getId());
    }

    @Override
    public int goodNumberUpdate(Goods goods, String name, int number) {
        String sql = "update goods set name=?,brand=?,price=?,number=?,createTime=?,deleteTime=?,isDel=? where name=?";
        return new ObjectUtil<Goods>().update(sql, goods.getName(), goods.getBrand(),
                goods.getPrice(), number, goods.getCreateTime(), goods.getDeleteTime(),
                goods.getIsDel(), name);
    }

    @Override
    public int goodPriceUpdate(Goods goods, String name, double price) {
        String sql = "update goods set name=?,brand=?,price=?,number=?,createTime=?,deleteTime=?,isDel=? where name=?";
        return new ObjectUtil<Goods>().update(sql, goods.getName(), goods.getBrand(),
                price, goods.getNumber(), goods.getCreateTime(), goods.getDeleteTime(),
                goods.getIsDel(), name);
    }

    @Override
    public int goodDelete(String name) {
        String sql = "delete from goods where name = ? ";
        return new ObjectUtil<Goods>().delete(sql,name);
    }
}
